package com.baizhi.lqy.service;

import com.baizhi.lqy.dao.UserMapper;
import com.baizhi.lqy.entity.Admin;
import com.baizhi.lqy.entity.User;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;



    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String, Object> queryAllPage(int page, int pageSize){
        Map<String, Object> map = new HashMap<>();
        User user = new User();
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);

        List<User> users = userMapper.selectByRowBounds(user,rowBounds);

        int totalCount = userMapper.selectCount(user);

        map.put("total",totalCount);
        map.put("page",page);
        map.put("rows",users);
        return map;
    }

    //查一个
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public User queryOne(int id) {
        User user = new User();
        user.setId(id);
        return userMapper.selectOne(user);
    }

    //修改状态
    public Map<String, Object> update(User user){
        Map<String, Object> map = new HashMap<>();
        User u = new User();
        u.setId(user.getId());
        User us = userMapper.selectOne(u);

        if(us.getStatus().equals("1")){
            us.setStatus("0");
            userMapper.updateByPrimaryKey(us);
            map.put("message","操作成功");
            map.put("status",200);
            return map;
        }else if (us.getStatus().equals("0")){
            us.setStatus("1");
            userMapper.updateByPrimaryKey(us);
            map.put("message","操作成功");
            map.put("status",200);
            return map;
        }else {
            map.put("message","操作失败");
            map.put("status",400);
            return map;
        }

    }

}
